package com.study.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.stat.TableStat.Mode;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Customer;
import com.study.services.CustomerService;
import com.study.services.HighSeasService;
import com.study.services.UserService;


@Controller
public class HighSeasController {
	@Autowired 
	HighSeasService hs;
	@Autowired
	UserService us;
	@Autowired
	CustomerService cs;
	/**
	 * 跳转至公海界面
	 */
	@RequestMapping("/gonghai")
    public ModelAndView getgh(Model model, HttpServletRequest request, HttpSession session) {

		String useid = request.getSession().getAttribute("useid").toString();
		ModelAndView mv = new ModelAndView("/customer/highseas");
		//查询公海数量以及名称
		List<JSONObject> high=hs.selecthigh(useid);

		mv.addObject("highs",high);
		return mv;
	}
	/**
	 * 新建公海
	 * @return
	 */
	@GetMapping("addhigh")
	public ModelAndView addDept() {
		System.out.println("新建公海进来了");
		ModelAndView mv = new ModelAndView("customer/addhighseas");
		JSONObject jsonObject = new JSONObject();
		mv.addObject("up", jsonObject);
		mv.addObject("colle",  new JSONObject());
		return mv;
	}
	
	/**
	 *公海分页的条件查询
	 * @param input
	 * @param selects
	 * @param currentPage
	 * @param pagesize
	 * @param request
	 * @return
	 */
	@PostMapping("customer/TJgh")
	@ResponseBody
	public String getghTJ(String input,String selects,int currentPage, int pagesize, HttpServletRequest request) {
		//先准备将要回归公海的客户回归公海
		
		
		String useid = request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = hs.getgh(currentPage, pagesize,input,selects,useid);
		for (JSONObject jsonObject : list) {
			System.out.println(jsonObject);
		}
		String a =null;
		System.out.println(input+"   "+selects+"    "+a);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		return json;
	}
	
	

    /**
     * 新建公海
     * @param name
     * @param day
     * @param num1
     * @param user
     * @return
     */
	@PostMapping("addhigh")
	@ResponseBody
	public ModelAndView addhigh(String name,String day,String num1,String user) {
		ModelAndView mv = new ModelAndView("/customer/customer");
		int result = hs.addhigh(name,day,num1,user);
		return mv;
	}
	
	

	/**
	 * 分配界面加载
	 * @param id
	 * @param session
	 * @return
	 */
	@GetMapping("allocat")
	public ModelAndView allocat(String id,HttpSession session) {
		System.out.println("分配进来了");
		ModelAndView mv = new ModelAndView("customer/allocat");
		session.setAttribute("allocat", id);
		List<JSONObject> list=us.getFindUserAll();
		mv.addObject("allocation", list);
		return mv;
	}
	/**
	 * 分配给具体某一个人
	 * @param user
	 * @param session
	 * @param request
	 * @return
	 */
	@PostMapping("alloc")
	@ResponseBody
	public boolean alloc(String user,HttpSession session,HttpServletRequest request) {
		//ModelAndView mv = new ModelAndView("customer/highseas");
		//获取具体公海客户的id
		String cusid = request.getSession().getAttribute("allocat").toString();
		return hs.upforHigh(cusid,user);
		
	}
	
	
}
